package com.shadow.demo.excel.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shadow.demo.data.excel.entity.DemoExcelEntity;
import com.shadow.demo.data.excel.mapper.DemoExcelMapper;
import com.shadow.demo.excel.demo.v3.Import3Listener;
import com.shadow.demo.excel.demo.v3.Import3VO;
import com.shadow.demo.excel.easy.excel.EasyExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Slf4j
@Service
public class DemoExcelService extends ServiceImpl<DemoExcelMapper, DemoExcelEntity> {

    @Resource
    private DemoExcelMapper demoExcelMapper;

    public Long getTotalData() {
        return demoExcelMapper.selectCount(null);
    }

    public List<DemoExcelEntity> getExcelData() {
        return demoExcelMapper.selectList(null);
    }

    /**
     * excel导入
     * v3
     * */
    public String import3(final MultipartFile excel) throws IOException {

        final Import3Listener<DemoExcelService, DemoExcelMapper, DemoExcelEntity, Import3VO> listener
                = new Import3Listener<>(this, Import3VO::convert);
        final String msg = EasyExcelUtil.importData(
                excel.getInputStream(),
                null,
                Import3VO.class,
                listener
        );
        log.info("import3 msg : {}", msg);
        return msg;
    }
}
